package com.ikingtech.platform.service.system.user.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikingtech.framework.sdk.context.security.Me;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.platform.service.system.user.entity.UserPostDO;
import com.ikingtech.platform.service.system.user.mapper.UserPostMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tie yan
 */
@Service
public class UserPostRepository extends ServiceImpl<UserPostMapper, UserPostDO> {

    public void removeByUserId(String userId, String tenantCode) {
        this.remove(Wrappers.<UserPostDO>query().lambda()
                .eq(UserPostDO::getUserId, userId)
                .eq(Tools.Str.isNotBlank(tenantCode), UserPostDO::getTenantCode, tenantCode));
    }

    public void removeByPostId(String postId) {
        this.remove(Wrappers.<UserPostDO>query().lambda()
                .eq(UserPostDO::getPostId, postId));
    }

    public List<String> listUserIdByPostIds(List<String> postIds) {
        return this.listObjs(Wrappers.<UserPostDO>lambdaQuery()
                .select(UserPostDO::getUserId)
                .in(UserPostDO::getPostId, postIds)
                .eq(UserPostDO::getTenantCode, Me.tenantCode()), String.class::cast);
    }

    public List<String> listUserIdByPostId(String postId) {
        return this.listObjs(Wrappers.<UserPostDO>lambdaQuery()
                .select(UserPostDO::getUserId)
                .eq(UserPostDO::getPostId, postId)
                .eq(UserPostDO::getTenantCode, Me.tenantCode()), String.class::cast);
    }

    public List<UserPostDO> listByTenantCodeAndUserIds(String tenantCode, List<String> userIds) {
        if (Tools.Coll.isBlank(userIds)) {
            return new ArrayList<>();
        }
        return this.list(Wrappers.<UserPostDO>lambdaQuery()
                .in(UserPostDO::getUserId, userIds)
                .eq(Tools.Str.isNotBlank(tenantCode), UserPostDO::getTenantCode, tenantCode));
    }

    public List<UserPostDO> listByUserId(String userId) {
        return this.list(Wrappers.<UserPostDO>lambdaQuery().eq(UserPostDO::getUserId, userId).eq(UserPostDO::getTenantCode, Me.tenantCode()));
    }

    public void removeByTenantCode(String tenantCode) {
        this.remove(Wrappers.<UserPostDO>lambdaQuery().eq(UserPostDO::getTenantCode, tenantCode));
    }

    public List<String> listPostIdByUserIds(List<String> userIds) {
        return this.listObjs(Wrappers.<UserPostDO>lambdaQuery().in(UserPostDO::getUserId, userIds), String.class::cast);
    }
}
